package com.smartEdu.service.impl.app;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.ClassStatic;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.NewsDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.NewsModel;
import com.smartEdu.service.NewsService;
import com.smartEdu.utils.JsonUtil;

import redis.clients.jedis.Jedis;

@Service(value="newsServiceImpl")
public class NewsServiceImpl extends BaseServiceImpl implements NewsService{
	@Autowired
	@Qualifier(value="newsDaoImpl")
	private NewsDao newsDao;
	/**
	 * 通过mybatis获取数据库信息
	 */
	@Override
	public void getNews(Session sess) {
		List<NewsModel> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			models=newsDao.getNews(map);//调用NewsDaoImpl中的getNews方法
			sess.put("news", models);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("News业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("News业务流程出错");
		}
		finally {
			map=null;
		}
	}
	/**
	 * 从Redis中获取要显示的新闻或者通知
	 */
	@Override
	public void getNewsFromRedis(Session sess) {
		CMap map=null;
		Jedis jedis=null;
		List<Object> list=new ArrayList<Object>();
		Map<String, String> redisMap=null;
		String s=null;
    	try{
    		map=SetToMap(sess);//将session中的request字段的内容经过JsonUtil类转化为map对象
    		jedis=ClassStatic.redis.getResource();//从redis连接池中获取redis连接对象
    		if (map.geti("ntype")==0) {
    			redisMap=jedis.hgetAll(Tag_Tips.NOTICE);//获取redis中名为"NOTICE"的map
    			s="notice";
			}
    		else if (map.geti("ntype")==1) {
    			redisMap=jedis.hgetAll(Tag_Tips.NEWS);//获取redis中名为"NEWS"的map
    			s="news";
			}
    		for (String key : redisMap.keySet()) {
				list.add(JsonUtil.toMap(redisMap.get(key)));//将map里的valuses放入list中
			}
    		sess.put(s, list);
    		sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
    	}
    	catch(Exception e){
    		LogCvt.error(e.getMessage(),e);
			throw new ServiceException("News业务流程出错");//流程出错要抛出ServiceException异常让contorller捕捉
    	}
    	finally {
    		if (redisMap!=null) {
				redisMap.clear();
				redisMap=null;
			}
    		if (jedis!=null) {
    			ClassStatic.redis.returnResource(jedis);//最好必须要释放所获取的redis对象
			}
    	}
	}
	@Override
	public void getNewsByNtype(Session sess) {
		// TODO Auto-generated method stub
		List<NewsModel> models=null;
	
		try {
			models=newsDao.getNewsByNtype();
			sess.put("news", models);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("News业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("News业务流程出错");
		}
	}

}
